from myelin.Ns import MYELINS

from Reset import Form


class Form(Form):
    
    def createInstance(self, context, node):
        eventData = context.event.data
        
        params = eventData.params
        errors = eventData.errors
        
        if eventData.errorMode:
            node.xml_set_attribute('error-mode', eventData.errorMode)
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        e_get = errors.get
        
        curNode = nxa(
            nxc(
                u'a:nick', MYELINS,
                content = unicode(params['nick'])
            )
        )
        e = e_get('nick')
        if e:
            curNode.error = e
            
        curNode = nxa(
            nxc(
                u'a:passwd', MYELINS
            )
        )
        e = e_get('passwd')
        if e:
            curNode.error = e
        
        # domain
        curNode = context.nodeType.createDomainOptions(
            context,
            nxa(
                nxc(
                    u'a:domain', MYELINS
                )
            ),
            params['domain']
        )
        e = e_get('domain')
        if e:
            curNode.error = e
        else:
            curNode.selected = unicode(params['domain'])
        
        defaults = context.defaults
        curNode = nxa(
            nxc(
                u'a:session-ttl', MYELINS,
                attributes = {
                    u'value'    : unicode(params['session-ttl']),
                    u'max'      : defaults.max_session_ttl,
                    u'increment': defaults.session_ttl,
                }
            )
        )
        e = e_get('session-ttl')
        if e:
            curNode.error = e
        
        return node